/*
 *  txdnet.cn tonydon
 * 
 */
package cn.txdnet.txcms.service;

import cn.txdnet.txcms.dao.HotWordDAO;
import cn.txdnet.txcms.domain.HotWord;
import cn.txdnet.txcms.dto.PageDTO;
import cn.txdnet.txcms.mapper.HotWordMapper;
import cn.txdnet.txcms.query.HotWordQuery;
import cn.txdnet.txcms.query.ViewHotWordQuery;
import cn.txdnet.txweb.basedao.FacadeProxy;
import cn.txdnet.txweb.basedao.mybatis.MybatisUtil;
import cn.txdnet.txweb.utils.CollectionUtil;
import cn.txdnet.txweb.utils.StringUtil;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import txdnet.util.StaticMark;

/**
 *
 * @author txdnet
 */
public class HotWordService {

    /**
     * 创建hotword
     *
     * @param hotword
     * @return
     */
    public static boolean make(HotWord hotword) {
        HotWordDAO dao = FacadeProxy.getManualCommitProxy(HotWordDAO.class);
        return dao.insert(hotword) > 0 ? true : false;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    public static int deleteBatch(String ids) {
        HotWordDAO dao = FacadeProxy.getManualCommitProxy(HotWordDAO.class);
        return dao.delete(StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
    }

    /**
     * 批量更新发布时间
     *
     * @param ids
     * @param pubtime
     * @return
     */
    public static int updatePubTime(String ids, long pubtime) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
        m.put("pubtime", pubtime);
        HotWordDAO dao = FacadeProxy.getManualCommitProxy(HotWordDAO.class);
        return dao.updatePubTime(m);
    }

    /**
     * 批量更新停止时间
     *
     * @param ids
     * @param stoptime
     * @return
     */
    public static int updateStopTime(String ids, long stoptime) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
        m.put("stoptime", stoptime);
        HotWordDAO dao = FacadeProxy.getManualCommitProxy(HotWordDAO.class);
        return dao.updateStopTime(m);
    }

    /**
     * 批量更新类目
     *
     * @param ids
     * @param cid
     * @param ctp
     * @return
     */
    public static int updateCatalogBatch(String ids, int cid, String ctp) {
        Map<String, Object> m = new HashMap<String, Object>(3);
        m.put("ida", StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
        m.put("cid", cid);
        m.put("ctp", ctp);
        HotWordDAO dao = FacadeProxy.getManualCommitProxy(HotWordDAO.class);
        return dao.updateCatalog(m);
    }

    /**
     * 更新顺序号
     *
     * @param id
     * @param ordernum
     * @return
     */
    public static boolean updateOrderNum(long id, long ordernum) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("id", id);
        m.put("ordernum", ordernum);
        HotWordDAO dao = FacadeProxy.getManualCommitProxy(HotWordDAO.class);
        return dao.updateOrderNum(m) > 0 ? true : false;
    }

    /**
     * 更新hotword
     *
     * @param hotword
     * @return
     */
    public static boolean updateHotWord(HotWord hotword) {
        HotWordDAO dao = FacadeProxy.getManualCommitProxy(HotWordDAO.class);
        return dao.updateHotWord(hotword) > 0 ? true : false;
    }

    /**
     * 查询最大排序号
     *
     * @return
     */
    public static long queryMaxOrderNum() {
        HotWordDAO dao = FacadeProxy.getAutoCommitProxy(HotWordDAO.class);
        return dao.queryMaxOrderNum();
    }

    /**
     * 查询hotword用于编辑
     *
     * @param id
     * @return
     */
    public static HotWord queryHotWordForEdit(long id) {
        HotWordDAO dao = FacadeProxy.getAutoCommitProxy(HotWordDAO.class);
        return dao.queryHotWordForEdit(id);
    }
    
    public static PageDTO queryByFactor(HotWordQuery query) {
        HotWordDAO dao = FacadeProxy.getAutoCommitProxy(HotWordDAO.class);
        return dao.queryByFactor(query);
    }

    // view service
    /**
     * 前端查询类目下hotword list
     *
     * @param query
     * @return
     */
    public static List<?> queryListForView(int cid, int crow, int listsize) {
        ViewHotWordQuery query = new ViewHotWordQuery();
        query.setCid(cid);
        query.setCrow(crow);
        query.setListsize(listsize);
        query.filter();
        HotWordDAO dao = FacadeProxy.getAutoCommitProxy(HotWordDAO.class);
        return dao.queryListForView(query);
    }

    /**
     * 前端点击更新
     *
     * @param id
     * @return
     */
    public static boolean increaseHitNum(long id) {
        ViewHotWordQuery query = new ViewHotWordQuery();
        query.setId(id);
        query.filter();
        HotWordDAO dao = FacadeProxy.getManualCommitProxy(HotWordDAO.class);
        return dao.increaseHitNum(query) > 0 ? true : false;
    }
}
